﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;
using YY.Common;

namespace YY.UI
{
    public partial class EditWorkflowFrm : BaseFrm
    {
        /// <summary>
        /// 费用所属工作单
        /// </summary>
        public TB_Order Order { get; set; }

        /// <summary>
        /// 用户源数据临时变量
        /// </summary>
        IEnumerable<TB_User> users = null;

        TB_Workflow wf = null;

        public EditWorkflowFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void EditWorkflowFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            this.cbBoxOperator.Enabled = false;
            this.users = this.BaseDataProxy.FindUsers(null, null);
            if (this.Order != null)
            {
                IEnumerable<TB_User> us = this.users.Where(p => p.role == (int)EnumCommon.SysRoleType.财务员 || p.role == (int)EnumCommon.SysRoleType.客服员);

                //int orderIntStatus = (int)Enum.Parse(t, this.Order.status);
                //if (orderIntStatus<3)

                Common.ComboBoxBindDatas<TB_User>(this.cbBoxOperator, us, "userName", "id");

                Common.ComboBoxBindDatas<TB_User>(this.cbBoxNextOperator, us, "userName", "id");
                this.cbBoxOperator.KeyPress += new KeyPressEventHandler(cbBoxOperator_KeyPress);

                loadCurrentWorkflow();
            }

            this.cbBoxMethod.SelectedIndex = 0;
        }

        /// <summary>
        /// 加载最新工作流
        /// </summary>
        private void loadCurrentWorkflow()
        {
            this.wf = this.BaseDataProxy.GetCurrentWorkflow(this.Order);
            if (wf != null)
            {
                //操作完成环节
                if (this.wf.flowStatus.Equals("操作完成") || this.wf.flowStatus.Equals("接单确认"))
                {
                    if (cbBoxMethod.Items.Count < 3)
                    {
                        this.cbBoxMethod.Items.Add("转派其他客服");
                    }
                }
                else
                {
                    if (cbBoxMethod.Items.Count >= 3)
                    {
                        this.cbBoxMethod.Items.Remove("转派其他客服");
                    }
                }

                this.txtBoxCode.Text = this.Order.code;
                this.txtBoxStatus.Text = this.wf.flowStatus;
                this.txtBoxComment.Text = wf.comment;
                if (wf.isHandle)
                    this.cbBoxOperator.SelectedItem = this.users.SingleOrDefault(p => p.id == wf.@operator);
                else
                    this.cbBoxOperator.SelectedItem = this.users.SingleOrDefault(p => p.id == SysLoginUserInfo.UserId);

                if (wf.nextOperator != null)
                    this.cbBoxNextOperator.SelectedItem = this.users.SingleOrDefault(p => p.id == wf.nextOperator);
                this.checkBox1.Checked = wf.isHandle;
                initBtn();
            }
            bindNextOP();
        }

        private void initBtn()
        {
            bool pass = false;
            if (this.Order.isSubmit)
            {
                if (SysLoginUserInfo.Role == (int)EnumCommon.SysRoleType.财务员)
                    pass = true;
            }
            else
            {
                if (SysLoginUserInfo.Role == (int)EnumCommon.SysRoleType.客服员)
                    pass = true;
            }
            this.btnConfirm.Enabled = !this.wf.isHandle && SysLoginUserInfo.UserId == this.wf.@operator;
            this.btnLZ.Enabled = (this.Order.status != EnumCommon.OrderStatus.已收款.ToString() || this.Order.status != EnumCommon.OrderStatus.取消.ToString()) && pass;
        }


        private void cbBoxOperator_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ComboBox cbBox = sender as ComboBox;
                if (this.users != null)
                {
                    TB_User[] users = this.users.Where(p => p.userName.Contains(cbBox.Text) || p.code.Contains(cbBox.Text)).ToArray();
                    Common.ComboBoxMatchUser(cbBox, users, this);
                }
            }
        }

        private void sendUpdateEvnt()
        {
            CommonEventArg<TB_Workflow> arg = new CommonEventArg<TB_Workflow>(this.wf, Common.OperateType.Update);
            YY.Common.EventCommon.OnWorkFlowUdDGVEvnt(this, arg);
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (this.wf.@operator != SysLoginUserInfo.UserId)
            {
                MessageBox.Show("不是环节处理人没有处理权限！");
                return;
            }

            //this.wf.isHandle = true;
            //this.wf.opTime = DateTime.Now;

            //this.wf.comment = this.txtBoxComment.Text;
            //if (this.BaseDataProxy.HandleWorkflow(wf))
            //{
            //    sendUpdateEvnt();
            //    MessageBox.Show("单据流程已被处理为 [" + wf.flowStatus + "] 状态");
            //    loadCurrentWorkflow();
            //}
            //else
            //    MessageBox.Show("处理异常！");

            MsgBoolean mb = this.BaseDataProxy.HandleWorkflow(this.Order.id, 0, null, null, this.txtBoxComment.Text);
            MessageBox.Show(mb.Msg);
            if (mb.Ok)
            {
                sendUpdateEvnt();
                loadCurrentWorkflow();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLZ_Click(object sender, EventArgs e)
        {
            if (this.cbBoxNextOperator.SelectedItem == null)
            {
                MessageBox.Show("请选择流转处理人！");
                this.cbBoxNextOperator.Focus();
                return;
            }

            Type t = typeof(EnumCommon.OrderStatus);
            int current = (int)Enum.Parse(t, this.wf.flowStatus);
            int next = current + 1;
            int nextOp = (this.cbBoxNextOperator.SelectedItem as TB_User).id;
            int opType = 1;
            if (this.cbBoxMethod.SelectedIndex == 1)
            {
                opType = -1;
                next = current - 1;
            }

            if (this.cbBoxMethod.SelectedIndex == 2)
            {
                opType = 2;
                next = current;
            }

            MsgBoolean mb = this.BaseDataProxy.HandleWorkflow(this.Order.id, opType, nextOp, Enum.GetName(t, next), this.txtBoxComment.Text);
            MessageBox.Show(mb.Msg);
            if (mb.Ok)
            {
                sendUpdateEvnt();
                loadCurrentWorkflow();
                this.Close();
            }
        }

        private void cbBoxMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindNextOP();
        }

        private void bindNextOP()
        {
            Type t = typeof(EnumCommon.OrderStatus);
            IEnumerable<TB_User> us = null;
            int workflowIntStatus = (int)Enum.Parse(t, this.wf.flowStatus);

            if (this.cbBoxMethod.SelectedIndex == 0)//下一环节
            {
                if (workflowIntStatus < 3 && !(workflowIntStatus == 2 && this.wf.isHandle))//客服处理
                {
                   us = this.users.Where(p => p.id == SysLoginUserInfo.UserId);
                }
                else//财务处理
                {
                    if (SysLoginUserInfo.Role == (int)EnumCommon.SysRoleType.财务员)
                        us = this.users.Where(p => p.id == SysLoginUserInfo.UserId);
                    else
                        us = this.users.Where(p => p.role == (int)EnumCommon.SysRoleType.财务员);
                }
            }
            else if(this.cbBoxMethod.SelectedIndex == 1)
            {
                if (workflowIntStatus == 3)//财务退回客服
                    us = this.users.Where(p => p.id == this.Order.agent);
                else//客服退回自己或财务退回自己
                    us = this.users.Where(p => p.id == SysLoginUserInfo.UserId);
            }
            else
            {
                us = this.users.Where(p => p.role == (int)EnumCommon.SysRoleType.客服员);
            }
            Common.ComboBoxBindDatas<TB_User>(this.cbBoxNextOperator, us, "userName", "id");
            this.cbBoxNextOperator.SelectedIndex = 0;
        }
    }
}
